﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace RestService.Processing
{
    public class Signal : DoiTuong
    {
        #region int AID property
        public int AID
        {
            get
            {
                if (_DataRow["AID"] is int) return (int)_DataRow["AID"];
                return 0;
            }
            set
            {
                _DataRow["AID"] = value;
            }
        }
        #endregion

        #region int PID property
        public int PID
        {
            get
            {
                if (_DataRow["PID"] is int) return (int)_DataRow["PID"];
                return 0;
            }
            set
            {
                _DataRow["PID"] = value;
            }
        }
        #endregion

        #region int RSS property
        public int RSS
        {
            get
            {
                if (_DataRow["RSS"] is int) return (int)_DataRow["RSS"];
                return 0;
            }
            set
            {
                _DataRow["RSS"] = value;
            }
        }
        #endregion

        public Signal(DataRow dataRow)
            : base(dataRow)
        {
            DanhMucSignal dm = new DanhMucSignal();
            //if (this.ID <= 0) this.ID = dm.GetNextID(); ;
        }
    }

    public class DanhMucSignal : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucSignal _Instance;
        public static DanhMucSignal Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucSignal();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                else _Instance.LoadFromDB();

                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "Signal";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new Signal((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public Signal this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new Signal(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public Signal NewDoiTuong()
        {
            return new Signal(DataTable.NewRow());
        }

        public Signal GetByProperty(string property, object value)
        {
            DataRow[] rows = GetDataRows(property, value);
            if (rows.Length > 0) return new Signal(rows[0]);
            else return null;
        }

        public Signal GetByID(int objID)
        {
            return GetByProperty("ID", objID);
        }

        public static DanhMucSignal GetAll()
        {
            return Instance;
        }

        public static DanhMucSignal Filter(string filterExpression)
        {
            try
            {
                DanhMucSignal obj = new DanhMucSignal();
                obj.DataView = new DataView(DanhMucSignal.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }
    }
}
